<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>论文列表</title>
    <link rel="stylesheet" href="css/list.css">
    <link rel="stylesheet" href="css/blog_list.css">
    <style>
        
        .nav{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 50px;
        }
        .container{
            padding-top: 80px;
            height: auto;
        }
        .container-right{
            width: auto;
        }
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
        .blog-pagnation-item{
            display: inline-block;
            padding: 8px;
            border: 1px solid #d0d0d5;
            color: #333;
        }
        .blog-pagnation-item:hover{
            background: #4e4eeb;
            color: #fff;
        }
        .blog-pagnation-item.actvie{
            background: #4e4eeb;
            color: #fff;
        }

    </style>
    <script src="js/jquery.min.js"></script>
    <script src="js/urltools.js"></script>
    <script src="js/sessiontools.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/logo2.jpg" alt="">
        <span class="title">我的论文系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_add.html">写论文</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 右侧内容详情 -->
        <div  class="container-right" style="width: 100%;">
            <!-- 每一篇论文包含标题, 摘要, 时间 -->
            <div id="artlist">

            </div>
            <hr>
            <div id="pageDiv" class="blog-pagnation-wrapper">
                <button onclick="toFirst()" class="blog-pagnation-item">首页</button>
                <button onclick="toPre()" class="blog-pagnation-item">上一页</button>
                <button onclick="toNext()" class="blog-pagnation-item">下一页</button>
                <button onclick="toLast()" class="blog-pagnation-item">末页</button>
            </div>
        </div>
    </div>
    <script>
        let pageIndex=1;//当前页码
        let pageSize=2;//一页展示的条数
        let pageCount=0;
        //初始化参数,当页面加载刷新的时候加载当前页面，当按钮的作用就是将正确的pageIndex和pageSize赋值
        function pageInit(){
            let paramPageIndex=getParamByKey("pageIndex");
            let paramPageSize=getParamByKey("paramPageSize");
            if (paramPageIndex!=null){
                pageIndex=paramPageIndex;
            }
            if (paramPageSize!=null){
                pageSize=paramPageSize;
            }
            doAjax();
        }
        pageInit();
        function toFirst(){
            if (pageIndex<=1){
                alert("当前已是首页");
                return;
            }
            pageIndex=1;
            location.href="blog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;
        }
        function toPre(){
            if (pageIndex<=1){
                alert("当前已是首页");
                return;
            }
            --pageIndex;
            location.href="blog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;//刷新当前页面
        }
        function toNext(){
            ++pageIndex;
            location.href="blog_list.html?pageIndex="+pageIndex+"&pageSize="+pageSize;//刷新当前页面
        }
        function toLast(){
            if (pageIndex>=pageCount){
                alert("抱歉你已经在末页啦!");
                return;
            }
            location.href="blog_list.html?pageIndex="+pageCount+"&pageSize="+pageSize;
        }
        function doAjax(){
            jQuery.ajax({
                url:"/article/getlistbypage",
                type:"POST",
                data:{"pageSize":pageSize,"pageIndex":pageIndex},
                success:function (res){
                    let artListHtml="";
                    if(res.code===200&&res.data.length>0){
                        for (let i = 0; i < res.data.length; i++) {
                            let item=res.data[i];
                            artListHtml+='<div class="blog">';
                            artListHtml+='<div class="title">'+item.title+'</div>';
                            artListHtml+='<div class="date">'+item.createtime+'</div>';
                            artListHtml+='<div class="desc">'+item.content+'</div>';
                            artListHtml+='<a href="blog_content.html?id='+item.id+'" class="detail">查看全文 &gt;&gt;</a>';
                            artListHtml+='</div>';

                        }
                        jQuery("#artlist").html(artListHtml);
                        jQuery("#pageDiv").show();
                    }else {
                        jQuery("#pageDiv").hide();
                        jQuery("#artlist").html("<h4>暂无数据</h4>");
                    }
                }
            })
        }
        //查询文章总条数
        jQuery.ajax({
            url: "/article/getcount",
            type: "POST",
            success:function (res){
                if (res.code===200&&res.data>0){
                    pageCount=Math.ceil(res.data/pageSize);//向上取整
                }
            }
        })
    </script>
</body>
</html>